package com.fang.transfer.file.listener;

import com.fang.transfer.file.ThreadContext;
import com.fang.transfer.file.common.code.ClientSendCode;
import com.fang.transfer.file.common.NettyHandle;
import com.fang.transfer.file.common.RequestListener;
import com.fang.transfer.file.common.code.ServiceSendCode;
import com.fang.transfer.file.common.model.User;
import com.fang.transfer.file.service.UserService;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * 获取在线用户
 */
@Service
public class GetOnlineUserListener extends RequestListener<Object> {


    @Autowired
    private NettyHandle nettyHandle;

    @Autowired
    private UserService userService;

    @Override
    public void handle(Object paramVoid) {
        ChannelHandlerContext channelHandlerContext = ThreadContext.getChannelHandlerContext();
        String id = channelHandlerContext.channel().id().toString();
        Collection<User> onlineUser = userService.getAllUser();
        nettyHandle.send(id, ServiceSendCode.RETURN_ONLINE_USER, onlineUser);
    }

    @Override
    public String getCode() {
        return ClientSendCode.GET_ALL_USER;
    }

}
